package com.ccit.springbootlaw.mapper;

import com.ccit.springbootlaw.pojo.CaseLawRelationship;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface CaseLawRelationshipMapper {

    // 根据ID查找案件法律关系
    @Select("SELECT * FROM case_law_relationship WHERE id = #{id}")
    CaseLawRelationship findById(long id);

    // 根据案件ID查找案件法律关系列表
    @Select("SELECT * FROM case_law_relationship WHERE case_id = #{caseId}")
    List<CaseLawRelationship> findByCaseId(int caseId);

    // 根据法律ID查找案件法律关系列表
    @Select("SELECT * FROM case_law_relationship WHERE law_id = #{lawId}")
    List<CaseLawRelationship> findByLawId(int lawId);

    // 插入新的案件法律关系
    @Insert("INSERT INTO case_law_relationship (case_id, law_id, created_at, updated_at) " +
            "VALUES (#{caseId}, #{lawId}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertCaseLawRelationship(CaseLawRelationship relationship);

    // 更新案件法律关系
    @Update("<script>" +
            "UPDATE case_law_relationship " +
            "<set>" +
            "<if test='caseId != null'>case_id = #{caseId},</if>" +
            "<if test='lawId != null'>law_id = #{lawId},</if>" +
            "updated_at = NOW()" +
            "</set>" +
            "WHERE id = #{id}" +
            "</script>")
    int updateCaseLawRelationship(CaseLawRelationship relationship);

    // 查询所有案件法律关系
    @Select("SELECT * FROM case_law_relationship")
    List<CaseLawRelationship> findAll();

    // 根据ID删除案件法律关系
    @Delete("DELETE FROM case_law_relationship WHERE id = #{id}")
    int deleteCaseLawRelationshipById(long id);
}